package socket.client;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8888);
        String ports = socket.getRemoteSocketAddress().toString();
        System.out.println(ports);
        OutputStream outputStream = socket.getOutputStream();
        //转化成输出字符流
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        //转化成输出缓冲流
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String nextLine = scanner.nextLine();
            bufferedWriter.write(nextLine);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            if(nextLine.equals("exit")){
                socket.close();
                break;
            }
        }

    }
}
